/**
 * 光标操作处理器
 * 处理光标移动、定位、选择等功能
 */
export class CursorOperations {
    constructor(editor) {
        this.editor = editor
    }

    /**
     * 光标移动到上一个单词
     */
    cursorWordLeft() {
        const text = this.editor.editor.value
        let pos = this.editor.editor.selectionStart

        // 向前跳过空白字符
        while (pos > 0 && /\s/.test(text[pos - 1])) {
            pos--
        }

        // 向前跳过单词字符
        while (pos > 0 && /\w/.test(text[pos - 1])) {
            pos--
        }

        this.editor.editor.selectionStart = this.editor.editor.selectionEnd = pos
    }

    /**
     * 光标移动到下一个单词
     */
    cursorWordRight() {
        const text = this.editor.editor.value
        let pos = this.editor.editor.selectionStart

        // 向后跳过单词字符
        while (pos < text.length && /\w/.test(text[pos])) {
            pos++
        }

        // 向后跳过空白字符
        while (pos < text.length && /\s/.test(text[pos])) {
            pos++
        }

        this.editor.editor.selectionStart = this.editor.editor.selectionEnd = pos
    }

    /**
     * 光标移动到行首
     */
    cursorHome() {
        const text = this.editor.editor.value
        const pos = this.editor.editor.selectionStart
        const lineStart = text.lastIndexOf('\n', pos - 1) + 1

        this.editor.editor.selectionStart = this.editor.editor.selectionEnd = lineStart
    }

    /**
     * 光标移动到行尾
     */
    cursorEnd() {
        const text = this.editor.editor.value
        const pos = this.editor.editor.selectionStart
        const lineEnd = text.indexOf('\n', pos)

        this.editor.editor.selectionStart = this.editor.editor.selectionEnd = 
            lineEnd === -1 ? text.length : lineEnd
    }

    /**
     * 光标移动到文档开始
     */
    cursorTop() {
        this.editor.editor.selectionStart = this.editor.editor.selectionEnd = 0
    }

    /**
     * 光标移动到文档结尾
     */
    cursorBottom() {
        const text = this.editor.editor.value
        this.editor.editor.selectionStart = this.editor.editor.selectionEnd = text.length
    }

    /**
     * 选中全部
     */
    selectAll() {
        this.editor.editor.select()
    }

    /**
     * 跳转到指定行
     * @param {number} lineNumber - 行号（1开始）
     * @returns {boolean} 是否成功跳转
     */
    goToLine(lineNumber) {
        if (!lineNumber || isNaN(lineNumber)) return false

        const lines = this.editor.editor.value.split('\n')
        const targetLine = Math.max(1, Math.min(parseInt(lineNumber), lines.length))

        // 计算位置
        let position = 0
        for (let i = 0; i < targetLine - 1; i++) {
            position += lines[i].length + 1
        }

        this.editor.editor.selectionStart = this.editor.editor.selectionEnd = position
        this.editor.editor.focus()

        return true
    }

    /**
     * 获取当前光标所在行号
     * @returns {number} 行号（1开始）
     */
    getCurrentLineNumber() {
        const text = this.editor.editor.value
        const pos = this.editor.editor.selectionStart
        return text.substring(0, pos).split('\n').length
    }

    /**
     * 获取当前光标所在列号
     * @returns {number} 列号（0开始）
     */
    getCurrentColumnNumber() {
        const text = this.editor.editor.value
        const pos = this.editor.editor.selectionStart
        const lineStart = text.lastIndexOf('\n', pos - 1) + 1
        return pos - lineStart
    }

    /**
     * 获取当前光标位置信息
     * @returns {Object} 位置信息 {line, column, position}
     */
    getCurrentPosition() {
        const pos = this.editor.editor.selectionStart
        const text = this.editor.editor.value
        const lineStart = text.lastIndexOf('\n', pos - 1) + 1
        const line = text.substring(0, pos).split('\n').length
        const column = pos - lineStart

        return {
            line,
            column,
            position: pos
        }
    }

    /**
     * 设置光标位置
     * @param {number} position - 位置
     */
    setPosition(position) {
        const text = this.editor.editor.value
        const safePosition = Math.max(0, Math.min(position, text.length))
        this.editor.editor.selectionStart = this.editor.editor.selectionEnd = safePosition
    }

    /**
     * 设置选择范围
     * @param {number} start - 开始位置
     * @param {number} end - 结束位置
     */
    setSelection(start, end) {
        const text = this.editor.editor.value
        const safeStart = Math.max(0, Math.min(start, text.length))
        const safeEnd = Math.max(safeStart, Math.min(end, text.length))
        
        this.editor.editor.selectionStart = safeStart
        this.editor.editor.selectionEnd = safeEnd
    }

    /**
     * 获取选中的文本
     * @returns {string} 选中的文本
     */
    getSelectedText() {
        const start = this.editor.editor.selectionStart
        const end = this.editor.editor.selectionEnd
        return this.editor.editor.value.substring(start, end)
    }

    /**
     * 检查是否有文本被选中
     * @returns {boolean} 是否有选中文本
     */
    hasSelection() {
        return this.editor.editor.selectionStart !== this.editor.editor.selectionEnd
    }

    /**
     * 获取当前行的文本
     * @returns {string} 当前行文本
     */
    getCurrentLineText() {
        const text = this.editor.editor.value
        const pos = this.editor.editor.selectionStart
        const lineStart = text.lastIndexOf('\n', pos - 1) + 1
        const lineEnd = text.indexOf('\n', pos)
        
        return text.substring(lineStart, lineEnd === -1 ? text.length : lineEnd)
    }

    /**
     * 获取指定行的文本
     * @param {number} lineNumber - 行号（1开始）
     * @returns {string|null} 行文本，如果行号无效则返回null
     */
    getLineText(lineNumber) {
        const lines = this.editor.editor.value.split('\n')
        if (lineNumber < 1 || lineNumber > lines.length) {
            return null
        }
        return lines[lineNumber - 1]
    }

    /**
     * 获取总行数
     * @returns {number} 总行数
     */
    getTotalLines() {
        return this.editor.editor.value.split('\n').length
    }
}